//
//  BCProjectDetailJoinRuleCell.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/22.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectDetailJoinRuleCell.h"

#import <OCSkeleton.h>

static CGFloat BCProjectDetailJoinRuleCellLabelPadding = 10.0;
static CGFloat BCProjectDetailJoinRuleCellTopPadding = 3.0;
static CGFloat BCProjectDetailJoinRuleCellBottomPadding = 7.0;

//
static CGFloat BCProjectDetailJoinRuleCellTtLabelWidth = 55.0;

@interface BCProjectDetailJoinRuleCell () <OCGradientsOwner>

@property (nonatomic, strong) UILabel *ttLabel;

/**
 *contentLabel
 */
@property (nonatomic, strong) UILabel *contentLabel;


@property (nonatomic, strong) OCGradientContainerView *ttContainerView;
@property (nonatomic, strong) OCGradientContainerView *contentContainerView;

@end

@implementation BCProjectDetailJoinRuleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.fd_enforceFrameLayout = true;
        [self setupSubviews];
        [self setContainerViews];
    }
    return self;
}
- (void)setupSubviews {
    self.ttLabel = [UILabel new];
    self.ttLabel.textColor = [UIColor qmui_colorWithHexString:@"#AEAEAE"];
    self.ttLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.ttLabel.numberOfLines = 0;
    [self.contentView addSubview:self.ttLabel];
    //
    self.contentLabel = [UILabel new];
    self.contentLabel.textColor = [UIColor qmui_colorWithHexString:@"#333333"];
    self.contentLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightRegular];
    self.contentLabel.numberOfLines = 0;
    [self.contentView addSubview:self.contentLabel];
    //
    [self.ttLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailJoinRuleCellTopPadding);
        make.left.mas_equalTo(self.contentView).offset(BCProjectDetailCellLeftPadding);
        make.width.mas_equalTo(BCProjectDetailJoinRuleCellTtLabelWidth);
        make.height.mas_greaterThanOrEqualTo(self.ttLabel.font.lineHeight);
    }];
    //
    CGFloat contentLeftPadding = BCProjectDetailCellLeftPadding+BCProjectDetailJoinRuleCellTtLabelWidth+BCProjectDetailJoinRuleCellLabelPadding;
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(BCProjectDetailJoinRuleCellTopPadding);
        make.left.mas_equalTo(self.contentView).offset(contentLeftPadding);
        make.right.mas_equalTo(-BCProjectDetailCellRightPadding);
        make.height.mas_greaterThanOrEqualTo(self.ttLabel.font.lineHeight);
    }];
}

- (void)setContainerViews {
    CGFloat ttRandomWidth = BCProjectDetailJoinRuleCellTtLabelWidth/2;
    NSInteger ttRandom = random()%((long)ttRandomWidth);
    ttRandomWidth = ttRandomWidth+ttRandom;
    
    self.ttContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.ttContainerView];
    [self.ttContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.ttLabel);
        make.height.mas_equalTo(self.ttLabel.font.lineHeight);
        make.width.mas_equalTo(ttRandomWidth);
    }];
    //
    CGFloat offset = random()%100;
    self.contentContainerView = [OCGradientContainerView new];
    [self.contentView addSubview:self.contentContainerView];
    [self.contentContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.contentLabel);
        make.height.mas_equalTo(self.contentLabel.font.lineHeight);
        make.width.mas_equalTo(self.contentLabel).offset(-offset);
    }];
}

- (CGSize)sizeThatFits:(CGSize)size {
    CGFloat height = 0;
    height += BCProjectDetailJoinRuleCellTopPadding;
    if (self.rule) {
        CGSize ttSize = [self.ttLabel sizeThatFits:CGSizeMake(BCProjectDetailJoinRuleCellTtLabelWidth, CGFLOAT_MAX)];
        //
        CGFloat contentLeftPadding = BCProjectDetailCellLeftPadding+BCProjectDetailJoinRuleCellTtLabelWidth+BCProjectDetailJoinRuleCellLabelPadding;
        CGSize contentFitsSize = CGSizeMake(size.width-contentLeftPadding-BCProjectDetailCellRightPadding, CGFLOAT_MAX);
        CGSize contentSize = [self.contentLabel sizeThatFits:contentFitsSize];
        //
        height += MAX(ttSize.height, contentSize.height);
    }else {
        height += MAX(self.contentLabel.font.lineHeight, self.ttLabel.font.lineHeight);
    }
    height += BCProjectDetailJoinRuleCellBottomPadding;
    return CGSizeMake(size.width, height);
}



- (void)setRule:(BCProjectDetailJoinRuleModel *)rule {
    _rule = rule;
    if (rule) {
        self.ttContainerView.alpha = 0;
        self.contentContainerView.alpha = 0;
        //
        self.ttLabel.text = rule.title;
        //
//        self.contentLabel.text = rule.content;
        NSString *content = rule.content?:@"";
        NSString *details = rule.details?:@"";
        NSString *string = [NSString stringWithFormat:@"%@%@",content,details];
        NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:string];
        NSRange detailRange = [string rangeOfString:details];
        if (detailRange.location!=NSNotFound) {
            [attri addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:11 weight:UIFontWeightRegular],NSForegroundColorAttributeName:[UIColor qmui_colorWithHexString:@"#A3A3A3"]} range:detailRange];
        }
        self.contentLabel.attributedText = attri;
    }else {
        self.ttContainerView.alpha = 1;
        self.contentContainerView.alpha = 1;
        //
        self.ttLabel.text = nil;
        self.contentLabel.attributedText = nil;
    }
}


#pragma mark - OCGradientsOwner
- (NSArray <OCGradientLayer *>*)gradientLayers {
    if (self.ttContainerView && self.contentContainerView) {
        return @[self.ttContainerView.gradientLayer,self.contentContainerView.gradientLayer];
    }
    return nil;
}

- (NSArray <UIView *>*)skeletonViews {
    if (self.ttContainerView && self.contentContainerView) {
        return @[self.ttContainerView,self.contentContainerView];
    }
    return nil;
}

@end
